package com.game.autoTool.project.web.common;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.List;

/**
 * 多目标识图-没有测试
 */
public class MultiTemplateMatching {
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args) {
        Mat source = Imgcodecs.imread("imageTemplate/识图案例.jpg");
        Mat template = Imgcodecs.imread("imageTemplate/游荡妖魔-水灵仙.jpg");
        
        // 执行模板匹配
        Mat result = new Mat();
        Imgproc.matchTemplate(source, template, result, Imgproc.TM_CCOEFF_NORMED);
        
        // 设置阈值
        double threshold = 0.8;
        List<Point> matches = new ArrayList<>();
        
        // 找到所有大于阈值的位置
        for(int x = 0; x < result.cols(); x++) {
            for(int y = 0; y < result.rows(); y++) {
                if(result.get(y, x)[0] >= threshold) {
                    matches.add(new Point(x, y));
                }
            }
        }
        
        // 绘制所有匹配位置
        for(Point p : matches) {
            Imgproc.rectangle(source, p, 
                new Point(p.x + template.cols(), p.y + template.rows()),
                new Scalar(0, 255, 0), 2);
        }
        
        Imgcodecs.imwrite("多目标匹配结果.jpg", source);
        System.out.println("找到 " + matches.size() + " 个匹配目标");
    }
}